package com.chaucer.service.impl;

import com.chaucer.pojo.Article;
import com.chaucer.repository.ArticleRepository;
import com.chaucer.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Optional;
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;
    @Override
    public Article findArticleById(Integer id) {
        Optional<Article> articleCheck = articleRepository.findById(id);
        return articleCheck.orElse(null);
    }

    @Override
    public Page<Article> queryArticlePage(Integer page, Integer size) {
        //构建分页条件
        Pageable pageable = PageRequest.of(page,size, Sort.by("id"));
        //返回分页查询结果
        return articleRepository.findAll(pageable);
    }
}
